package com.jule.miaozhuapc.controller;


import com.jule.miaozhuapc.ano.Auth;
import com.jule.miaozhuapc.ano.TokenUser;
import com.jule.miaozhuapc.domain.WawaRoom;
import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.dto.RoomDetail;
import com.jule.miaozhuapc.dto.RoomDto;
import com.jule.miaozhuapc.dto.WsUtil;
import com.jule.miaozhuapc.service.RoomService;
import com.jule.miaozhuapc.service.UserService;
import com.jule.miaozhuapc.util.Const;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@RequestMapping( value = Const.WAWA_HTTP_URL_V1+ "/room")
@Controller
public class RoomController {

    private Logger logger = Logger.getLogger(RoomController.class);
    //private   Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RoomService roomService;
    @Autowired
    private UserService userService;

    /**
     * 拉取所有游戏房间
     *
     * @return
     */
    @RequestMapping(value = "/getRooms", method = RequestMethod.GET)
    public @ResponseBody
    WsUtil getAllRoom() {
        List<RoomDto> roomDtos = new ArrayList<>();
        List<WawaRoom> rooms = roomService.findAllRoom();
        if (rooms != null && rooms.size() > 0 ){
            for (WawaRoom room : rooms){
                RoomDto roomDto = new RoomDto();
                roomDto.setChatRoomId(room.getChatRoomId());
                roomDto.setCoin(room.getCoin());
                roomDto.setGameTime(room.getGameTime());
                roomDto.setRoomId(room.getId());
                roomDto.setRoomName(room.getRoomName());
                roomDto.setRoomPic(room.getRoomPic());
                roomDto.setWawaId(room.getWawaId());
                //TODO 默认没有用
                roomDto.setHasUse(false);
                roomDtos.add(roomDto);
            }
        }
        WsUtil ws = new WsUtil();
        ws.setBody(roomDtos);
        //logger.info(new JSONObject(ws));
        return ws;
    }

    /**
     * h5 拉去房间详情 包含自己的金币信息
     *
     * @param user
     * @return
     */
    @Auth
    @RequestMapping(value = "/roomDetail", method = RequestMethod.GET)
    public @ResponseBody
    WsUtil RoomDetail(@TokenUser WawaUser user,
                      @RequestParam(name = "roomId", required = true) String roomId) {
        RoomDetail roomDetail = roomService.findRoomDetailAndUser(Long.parseLong(roomId), user.getId());

        WsUtil ws = new WsUtil();
        ws.setBody(roomDetail);
        return ws;
    }
}
